package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeTemplateNewDTO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Api(tags = "02. 属性模板管理模块")
@RestController
@RequestMapping("/attributeTemplates")
public class AttributeTemplateController {

    @Autowired
    IAttributeTemplateService attributeTemplateService;

    public AttributeTemplateController() {
        System.out.println("创建控制器类对象：AttributeTemplateController");
    }

    @PostMapping("/add-new")
    @ApiOperation("添加属性模板")//将添加属性模板替换成addNew
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated AttributeTemplateNewDTO attributeTemplateNewDTO){
        /*try{
            attributeTemplateService.addNew(attributeTemplateNewDTO);
            return "添加属性模板成功!";
        }catch (RuntimeException e){
            return "添加属性模板失败,尝试添加的属性模板名称已被占用!";
        }*/
        log.debug("开始处理[属性模板]的请求,参数: {}", attributeTemplateNewDTO);
        attributeTemplateService.addNew(attributeTemplateNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8080/attributeTemplates/delete?id=1
    @ApiOperation("删除属性模板")
    @ApiOperationSupport(order = 200)
    @PostMapping("/delete")
    public JsonResult delete(Long id){
        log.debug("开始处理[删除属性模板数据]的请求,参数:{}",id);
        attributeTemplateService.delete(id);
        return JsonResult.ok();
    }
}
